import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LKQ
 * @date 2021/12/30 8:16
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] hand = {1, 2, 3, 4, 5};
        solution.isNStraightHand(hand, 4);
    }

    public boolean isNStraightHand(int[] hand, int groupSize) {
        int n = hand.length;
        if (n % groupSize != 0) {
            return false;
        }
        Map<Integer, Integer> map = new HashMap<>();
        Arrays.sort(hand);
        for (int j : hand) {
            map.put(j, map.getOrDefault(j, 0) + 1);
        }
        for (int i = 0; i < n; ) {
            if (map.get(hand[i]) == 0) {
                i++;
                continue;
            }
            int start = groupSize;
            while (start > 0) {
                int temp = hand[i] + groupSize - start;
                map.put(temp, map.getOrDefault(temp, 0) - 1);
                if (map.getOrDefault(temp, -1) < 0) {
                    return false;
                }
                start--;
            }
        }
        return true;
    }
}
